package com.car.alliance.ui.home

import android.Manifest
import android.app.Activity
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.baidu.location.BDLocation
import com.baidu.location.BDLocationListener
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MapStatus
import com.baidu.mapapi.map.MapStatusUpdateFactory
import com.baidu.mapapi.map.MyLocationData
import com.baidu.mapapi.model.LatLng
import com.car.alliance.R
import com.car.alliance.base.BaseFragment
import kotlinx.android.synthetic.main.fragment_home_map.*
import java.util.*


class HomeMapFragment : BaseFragment(), BaiduMap.OnMapStatusChangeListener {
    private var mTitle: String? = null
    internal var mLocClient: LocationClient? = null
    var myListener = MyLocationListenner()
    private var locData: MyLocationData? = null
    private var mCurrentDirection = 0
    private var mCurrentLat = 0.0
    private var mCurrentLon = 0.0
    var isFirstLoc = true


    companion object {
        fun getInstance(title: String): HomeMapFragment {
            val fragment = HomeMapFragment()
            val bundle = Bundle()
            fragment.arguments = bundle
            fragment.mTitle = title
            return fragment
        }
    }

    override fun getLayoutId(): Int = R.layout.fragment_home_map

    override fun initView() {
        getManifest()
        setMapsetting()
    }

    override fun lazyLoad() {

    }

    fun  getManifest(){
        val permissionList: MutableList<String> = ArrayList()
        if (ContextCompat.checkSelfPermission(context as Activity, Manifest.permission.ACCESS_FINE_LOCATION) !== PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION)
        }
        if (ContextCompat.checkSelfPermission(context as Activity, Manifest.permission.READ_PHONE_STATE) !== PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.READ_PHONE_STATE)
        }
        if (ContextCompat.checkSelfPermission(context as Activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) !== PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
        }
        if (!permissionList.isEmpty()) {
            val permissions = permissionList.toTypedArray()
            ActivityCompat.requestPermissions(context as Activity, permissions, 1)
        }

    }

    fun setMapsetting(){
        bmapView.map.isMyLocationEnabled = true
        mLocClient = LocationClient(context)
        mLocClient!!.registerLocationListener(myListener)
        val option = LocationClientOption()
        option.isOpenGps = true // 打开gps
        option.coorType = "bd09ll"// 设置坐标类型
        option.setScanSpan(1000)
        mLocClient!!.setLocOption(option)
        bmapView.showZoomControls(false) //隐藏地图放大缩小按钮
        bmapView.map.setOnMapStatusChangeListener(this) //添加地图移动监听
        //注册LocationListener监听器
        val myLocationListener = MyLocationListenner()
        mLocClient!!.registerLocationListener(myLocationListener)
        //开启地图定位图层
        mLocClient!!.start()

        img_view_locatio.setOnClickListener {
            val pt = LatLng(mCurrentLat, mCurrentLon)
            bmapView.map.clear()
            bmapView.map.setMapStatus(MapStatusUpdateFactory.newLatLng(pt)) //把地图中心点移动到平pt1坐标
        }
    }

    override fun onResume() {
        super.onResume()
                //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        bmapView!!.onResume()
//        mMapView!!.onResume()
    }

    override fun onPause() {
        super.onPause()
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        bmapView!!.onPause()

    }

     override fun onDestroy() {
         mLocClient!!.stop();
         bmapView.map.isMyLocationEnabled = false
         bmapView.onDestroy();
//         bmapView.map = null;
        super.onDestroy()
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        bmapView!!.onDestroy()
    }


    /**
     * 定位SDK监听函数
     */
    inner class MyLocationListenner : BDLocationListener {
        override fun onReceiveLocation(location: BDLocation?) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || bmapView == null) {
                return
            }
            mCurrentLat = location.latitude
            mCurrentLon = location.longitude
            var locData = MyLocationData.Builder()
                    .accuracy(location.radius) // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(location.direction).latitude(location.latitude)
                    .longitude(location.longitude).build()
            bmapView.map.setMyLocationData(locData)
            if (isFirstLoc) {
                isFirstLoc = false
                val ll = LatLng(location.latitude, location.longitude)
                val builder = MapStatus.Builder()
                builder.target(ll).zoom(18.0f)//定位 缩放到18.f级
                bmapView.map.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()))
            }
        }

        fun onReceivePoi(poiLocation: BDLocation) {}
    }

    override fun onMapStatusChangeStart(p0: MapStatus?) {
    }

    override fun onMapStatusChangeStart(p0: MapStatus?, p1: Int) {
    }

    override fun onMapStatusChange(p0: MapStatus?) {
    }

    override fun onMapStatusChangeFinish(p0: MapStatus?) {
    }

}


